package com.chinacoal.microservice.service.impl;

import com.chinacoal.microservice.mapper.CcmsSmartleasingDeviceManagementMapper;
import com.chinacoal.microservice.mapper.CcmsSmartleasingHistoricalPaymentMapper;
import com.chinacoal.microservice.mapper.CcmsSmartleasingRegionalAnalysisMapper;
import com.chinacoal.microservice.mapper.DataCenterMapper;
import com.chinacoal.microservice.pojo.CcmsSmartleasingDeviceManagement;
import com.chinacoal.microservice.pojo.CcmsSmartleasingHistoricalPayment;
import com.chinacoal.microservice.pojo.CcmsSmartleasingRegionalAnalysis;
import com.chinacoal.microservice.pojo.DataCenter;
import com.chinacoal.microservice.service.DataCenterService;
import com.chinacoal.microservice.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

/**
 * @author zhuliang
 * @version V1.0
 * @Description: 数据中心和可视化页面-后端实现
 * @date 2020-12-29 10：23
 */
@Service
public class DataCenterServiceImpl implements DataCenterService {

    @Autowired
    private DataCenterMapper dataCenterMapper;

    @Autowired
    private CcmsSmartleasingHistoricalPaymentMapper historicalPaymentMapper;

    @Autowired
    private CcmsSmartleasingRegionalAnalysisMapper regionalAnalysisMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private CcmsSmartleasingDeviceManagementMapper deviceManagementMapper;

    /**
     * @Description: 费用概况
     * @author zhuliang
     * @date 2020/12/29 10:26
     */
    @Override
    public Result<DataCenter> costOverview() {
        //查询合同数和租户数
        DataCenter center = dataCenterMapper.getData();

        //查询费用概况信息
        DataCenter center2 = dataCenterMapper.getCost();

        //查询出租数，自用数，空置数，预定数，房间总数
        DataCenter roomOccupancyRate = dataCenterMapper.getRoomOccupancyRate();

        //写入刷新时间
        center.setRefreshTime(new Date());

        if (null != center2) {

            if (null != center2.getExpensesReceivable()) {
                //应收费用
                center.setExpensesReceivable(center2.getExpensesReceivable());
            }

            if (null != center2.getPaidExpenses()) {
                //计算欠费
                center.setAccumulatedArrears(center2.getExpensesReceivable().subtract(center2.getPaidExpenses()));

                //计算收款完成率
                center.setProgressCompleted((center2.getPaidExpenses().divide(center2.getExpensesReceivable(), RoundingMode.HALF_UP)).multiply(new BigDecimal(100)).setScale(0, BigDecimal.ROUND_HALF_UP));

                //实收费用
                center.setPaidExpenses(center2.getPaidExpenses());
            }
        }

        //计算出租率
        center.setLeaseRate((int) Math.round(roomOccupancyRate.getLeaseNumber() * 1.0 / roomOccupancyRate.getRoomNumber() * 100));

        //计算自用率
        center.setSelfUseRate((int) Math.round(roomOccupancyRate.getSelfUseNumber() * 1.0 / roomOccupancyRate.getRoomNumber() * 100));

        //计算空置率
        center.setVacancyRate((int) Math.round(roomOccupancyRate.getVacancyNumber() * 1.0 / roomOccupancyRate.getRoomNumber() * 100));

        //计算预定率
        center.setPredeterminedRate((int) Math.round(roomOccupancyRate.getPredeterminedNumber() * 1.0 / roomOccupancyRate.getRoomNumber() * 100));

        //计算办公率
        center.setOfficeRate((int) Math.round(roomOccupancyRate.getOfficeNumber() * 1.0 / roomOccupancyRate.getRoomNumber() * 100));

        return Result.success(center);
    }

    /**
     * @Description: 得到综合分析数据
     * @author zhuliang
     * @date 2020/12/31 10:25
     */
    @Override
    public Result<List<CcmsSmartleasingHistoricalPayment>> getComprehensiveAnalysisData(String year, Integer type) {
        //得到需要查询的时间区间
        String dateStart = null;
        String dateEnd  = null;
        if (0 == type) {        //全季度
            dateStart = year + "-01";
            dateEnd = year + "-12";
        }else if (1 == type) {  //第一季度
            dateStart = year + "-01";
            dateEnd = year + "-03";
        }else if (2 == type) {  //第二季度
            dateStart = year + "-04";
            dateEnd = year + "-06";
        }else if (3 == type) {  //第三季度
            dateStart = year + "-07";
            dateEnd = year + "-09";
        }else if (4 == type) {  //第四季度
            dateStart = year + "-10";
            dateEnd = year + "-12";
        }

        //开始查询
        List<CcmsSmartleasingHistoricalPayment> historicalPayments = historicalPaymentMapper.getAll(dateStart, dateEnd);

        return Result.success(historicalPayments);
    }

    /**
     * @Description: 得到区域分析数据
     * @author zhuliang
     * @date 2020/12/31 10:25
     */
    @Override
    public Result<List<CcmsSmartleasingRegionalAnalysis>> getAreaAnalysisData(Integer type) {
        //判断查询类型
        Integer pageSize = null;
        if (0 == type) {
            pageSize = 5;
        }

        //查询区域分析数据
        List<CcmsSmartleasingRegionalAnalysis> regionalAnalyses = regionalAnalysisMapper.getAll(pageSize);

        return Result.success(regionalAnalyses);
    }

    /**
     * @Description: 得到房间面积数据
     * @author zhuliang
     * @date 2021/1/8 10:04
     */
    @Override
    public Result<DataCenter> getAreaData(Integer type) {
        //判断是查询还是刷新数据
        DataCenter areaData = null;
        if (0 == type) {
            //从redis查询数据
            areaData = (DataCenter) redisTemplate.opsForValue().get("areaData");
        }

        //判断是否为空
        if (1 == type || "null".equals(areaData) || null == areaData || "".equals(areaData)) {
            //查询房间面积数据
            areaData = dataCenterMapper.getAreaData();

            if (null == areaData) {
                return Result.success(areaData);
            }

            //计算其他面积
            areaData.setOtherArea(areaData.getTotalArea().subtract(areaData.getPropertyArea()).subtract(areaData.getCommissionedArea()));

            //将数据写入到redis
            redisTemplate.opsForValue().set("areaData", areaData);
        }

        return Result.success(areaData);
    }

    /**
     * @Description: 可视化页面房间统计数据
     * @author zhuliang
     * @date 2021/1/11 16:28
     */
    @Override
    public Result<List<CcmsSmartleasingDeviceManagement>> getVisualizationRoomData() {
        //查询需要展示的房间信息
        List<CcmsSmartleasingDeviceManagement> roomData = deviceManagementMapper.getAllRoomData();

        //遍历房间

        for (CcmsSmartleasingDeviceManagement data : roomData) {
            //获取其断路器信息
            List<CcmsSmartleasingDeviceManagement> lineStatusData = deviceManagementMapper.getLineStatus(data.getRegion());

            data.setLineStatusData(lineStatusData);
        }

        return Result.success(roomData);
    }

    /**
     * @Description: 得到所有有记录的年份
     * @author zhuliang
     * @date 2021/1/15 16:22
     */
    @Override
    public Result<List<String>> getAllByYear() {
        //查询所有的年份
        List<String> allYear = historicalPaymentMapper.getAllYear();

        return Result.success(allYear);
    }

    /**
     * @Description: 可视化页面房间统计数据（右）
     * @author zhuliang
     * @date 2021/1/26 18:39
     */
    @Override
    public Result<CcmsSmartleasingDeviceManagement> getVisualizationStatisticalData() {
        CcmsSmartleasingDeviceManagement statisticalData = deviceManagementMapper.getAllStatisticalData();

        return Result.success(statisticalData);
    }
}
